/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function($,IO,jui,upui,up,bootbox,definebt) {
    //医院的表格
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '医院名称',
                dataIndex: 'hospital_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].hospital_name + "</span>";
                }
            },
            {
                title: '医院所在省',
                dataIndex: 'province_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].province_name + "</span>";
                }
            },
            {
                title: '所在市',
                dataIndex: 'city_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].city_name + "</span>";
                }
            },
            {
                title: '所在区',
                dataIndex: 'area_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].area_name + "</span>";
                }
            },
            {
                title: '医院级别',
                dataIndex: 'hospital_level',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].hospital_level.hospital_levelname + "</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='javascript:void(0)' class='detial' act_id='"+data[datarow].hospital_id+"'><span style='color: #6ab7ad;'>" + "详情" + "</span></a>"
                        + "&nbsp;<a href='javascript:void(0)' class = 'edit' act_id = '"+data[datarow].hospital_id+"'><span style='color: #d47c7b;'>" + "编辑</span></a>";
                }
            },

        ];


        //数组初始化

        function dataInit() {
            var province = $("#category1 option:selected").attr("data-id");
            var city = $("#category2 option:selected").val();
            var area = $("#category3 option:selected").val();
            var hosptiallevel = $("#category4 option:selected").attr("data-id");
            grid = new Grid("/96053/api/hostpital?province_id="+province+"&city_id="+city+"&area_id="+area+"&hospital_levelid="+hosptiallevel, $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var content=$("#citiao_content").val();
            var param="content=" +content;
            return param;
        }
        dataInit();

        //查询
        $("#query").bind("click", function () {
            dataInit();
        });

        //医院列表的省市区的三级联动

        $("#category1").on("change",function(){
            //console.log($(this).val());
            document.reg_testdate.category22.innerHTML='请选择';
            document.reg_testdate.category22.options.add(new Option("请选择","请选择"));
            document.reg_testdate.category33.innerHTML='请选择';
            document.reg_testdate.category33.options.add(new Option("请选择","请选择"));
            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == $(this).val()){
                    //console.log("测试1 ",$(this).val());
                    //console.log("测试2",provinceList[obj].cityList);
                    for (var x in provinceList[obj].cityList){
                        var city = provinceList[obj].cityList;
                        document.reg_testdate.category22.options.add(new Option(city[x].name,city[x].code));
                        //console.log("城市们",city[x].name);
                    }
                }
            }
        })
        $("#category2").on("change",function(){
            //console.log($(this).val());
            //console.log("选中的省",$("#category1 option:selected").val());
            document.reg_testdate.category33.innerHTML='请选择';
            document.reg_testdate.category33.options.add(new Option("请选择","请选择"));
            var province = $("#category1 option:selected").val();
            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == province){
                    //console.log("测试1 ",$(this).val());
                    //console.log("测试2",provinceList[obj].cityList);
                    var city = provinceList[obj].cityList;
                    for (var x in city){
                        if ($("#category2 option:selected").text()==city[x].name){
                            var area = city[x].areaList;
                            for (var y in area){
                                document.reg_testdate.category33.options.add(new Option(area[y].name,area[y].code));
                                //console.log("城市们",area[y].name);
                            }

                        }

                    }
                }
            }
        })

    });

    //添加医院
    $("#addhosptial").bind("click", function () {
        $("#addHosptialModal").modal();
    });

    //添加科室
    $("#addDepartment").bind("click", function () {
        $("#addDepartments").modal();
        //$("#departments").html('<div style="float: left;padding-left: 10px"><p><input type="checkbox" name="department" value="" />口腔科 </p></div>');

    });
    $("#commitDepartments").bind("click",function(){
        var newoption = '';
            newoption = '<div style="float: left;padding-right: 10px"><p><input type="checkbox" name="department" is_del=false value="" />'+$("#addDepartmentName").val()+' </p></div>';
        $("#departments").append(newoption);
    })

    //删除科室
    $(".deleteDepartment").bind("click", function () {
        var department = document.getElementsByName("department");
        var count = department.length;
        if (!confirm("确认删除选中项？")) {
            window.event.returnValue = false;
        }else{
            for (var i = count-1;i >= 0;i--){
                if (department[i].checked){
                    //console.log(department)
                    $(department[i]).attr("is_del",true)
                    $(department[i]).parent().parent().hide()
                    //console.log("dd",$(department[i]).attr("is_del"))
                    //$(department[i]).parent().parent().remove();
                }

            }
        }
    });

    //删除编辑医院里的科室
    $("#deleteDepartment").bind("click", function () {
        var department = document.getElementsByName("department2");
        var count = department.length;
        console.log("删除科室",department);
        if (!confirm("确认删除选中项？")) {
            window.event.returnValue = false;
        }else{
            for (var i = count-1;i >= 0;i--){
                if (department[i].checked){
                    console.log(department[i])
                    $(department[i]).attr("is_del",true)
                    $(department[i]).parent().parent().hide()
                    //console.log("dd",$(department[i]).attr("is_del"))
                    //$(department[i]).parent().parent().remove();
                }

            }
        }
    });

    //添加职称
    $("#addDuty").bind("click", function () {
        $("#addDutys").modal();
        //$("#departments").html('<div style="float: left;padding-left: 10px"><p><input type="checkbox" name="department" value="" />口腔科 </p></div>');

    });
    $("#commitDutys").bind("click",function(){
        var newoption = '';
        newoption = '<div style="float: left;padding-right: 10px"><p><input type="checkbox" name="duty" value="" is_del=false />'+$("#addDutyName").val()+' </p></div>';
        $("#dutys").append(newoption);
    })

    //删除职称
    $(".deleteDuty").bind("click", function () {
        var duty = document.getElementsByName("duty");
        //console.log(duty);
        if (!confirm("确认删除选中项？")) {
            window.event.returnValue = false;
        }else{
            for (var i = duty.length-1;i >= 0;i--){
                if (duty[i].checked){
                    //$(duty[i]).parent().parent().remove();
                    $(duty[i]).attr("is_del",true)
                    $(duty[i]).parent().parent().hide()
                }
            }
        }

    });

    //删除编辑医院里的职称
    $("#deleteDuty").bind("click", function () {
        var duty = document.getElementsByName("duty2");
        //console.log(duty);
        if (!confirm("确认删除选中项？")) {
            window.event.returnValue = false;
        }else{
            for (var i = duty.length-1;i >= 0;i--){
                if (duty[i].checked){
                    //$(duty[i]).parent().parent().remove();
                    $(duty[i]).attr("is_del",true)
                    $(duty[i]).parent().parent().hide()
                }
            }
        }

    });

    //添加医院的省市区的三级联动

    $("#category5").on("change",function(){
        //console.log($(this).val());
        document.reg_testdate2.category62.innerHTML='请选择';
        var provinceList = JSON.parse($("#province").val());
        var obj;
        for (obj in provinceList){
            if(provinceList[obj].name == $(this).val()){
                document.reg_testdate2.category62.options.add(new Option("请选择","请选择"));
                //console.log("测试1 ",$(this).val());
                //console.log("测试2",provinceList[obj].cityList);
                for (var x in provinceList[obj].cityList){
                    var city = provinceList[obj].cityList;
                    document.reg_testdate2.category62.options.add(new Option(city[x].name,city[x].code));
                    //console.log("城市们",city[x].name);
                }
            }
        }
    })
    $("#category6").on("change",function(){
        //console.log("市",$(this).val());
        //console.log("哈哈",$("#category5 option:selected").text());
        var province = $("#category5 option:selected").text();
        document.reg_testdate2.category73.innerHTML='请选择';
        var provinceList = JSON.parse($("#province").val());
        var obj;
        for (obj in provinceList){
            if(provinceList[obj].name == province){
                document.reg_testdate2.category73.options.add(new Option("请选择","请选择"));
                //console.log("测试1 ",$(this).val());
                //console.log("测试2",provinceList[obj].cityList);
                var city = provinceList[obj].cityList;
                for (var x in city){
                    if ($("#category6 option:selected").text()==city[x].name){
                        var area = city[x].areaList;
                        for (var y in area){
                            document.reg_testdate2.category73.options.add(new Option(area[y].name,area[y].code));
                            //console.log("城市们",area[y].name);
                        }

                    }

                }
            }
        }
    })

    //医院照片上传
    $('#fileupload1').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs3').src = '/images/loading.gif';
    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';
        console.log("#upimgs3" + alt);
        $("#upimgs3" + alt).attr("src", data._response.result.url);

    });
    //编辑医院的照片上传
    $('#fileupload2').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs4').src = '/images/loading.gif';
    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';
        console.log("#upimgs4" + alt);
        $("#upimgs4" + alt).attr("src", data._response.result.url);

    });

    //添加医院保存
    $("#commitHosptial").bind("click",function(){
        //console.log("dataSrc:",$("#upimgs3").attr("src"));
        //var localStorageData= eval("(" + localStorage.getItem('data') + ")");
        //var admin_id = localStorageData.data.admin_id;
        //console.log('admin_id',admin_id);
        //var admin_id = '1';
        var hospital_pic = $("#upimgs3").attr("src");
        var address = $("#AddhospitalAddress").val();
        var hospital_desc = document.getElementById("hospitalInstro").value;
        var process = document.getElementById("hospitalProcess").value;
        var hospital_name = $("#Addhospitalname").val();
        var hospital_phone = $("#Addhospitalphone").val();
        //var hospital_levelid = $("#Addhospitallevel").val();
        var hospital_levelid = $("#category8 option:selected").attr("data-id");
        var newcard_ability = $("#category85 option:selected").attr("data-id");
        var province_id = $("#category5 option:selected").attr("data-id");
        var city_id = $("#category6 option:selected").val();
        var area_id = $("#category7 option:selected").val();
        var department = document.getElementsByName("department");
        //console.log("haha",newcard_ability);
        var departmentStr='',dutyStr='';
        for (var i = 0;i <department.length;i++){
            if (department[i].checked){
                departmentStr = departmentStr + department[i].nextSibling.nodeValue +";";
            }
        }
        departmentStr = departmentStr.substring(0,departmentStr.length-1);
        var duty = document.getElementsByName("duty");
        for (var i = 0;i <duty.length;i++){
            if (duty[i].checked){
                dutyStr = dutyStr + duty[i].nextSibling.nodeValue +";";
            }
        }
        dutyStr = dutyStr.substring(0,dutyStr.length-1);
        //console.log("科室与职称",departmentStr,dutyStr);

        if (hospital_name == ''){
            alert("请填写医院名称");
            return;
        }
        if (hospital_phone == ''){
            alert("请填写医院电话");
            return;
        }
        if ($("#category8 option:selected").attr("data-id") == undefined){
            alert("请选择医院级别");
            return;
        }
        if (newcard_ability == undefined){
            alert("请选择建卡能力");
            return;
        }
        if ($("#category5 option:selected").attr("data-id") == undefined){
            alert("请选择医院所在省");
            return;
        }
        if ($("#category6 option:selected").val() == '请选择'){
            alert("请选择医院所在市");
            return;
        }
        if ($("#category7 option:selected").val() == '请选择'){
            alert("请选择医院所在区");
            return;
        }
        if (address == ''){
            alert("请输入详细地址");
            return;
        }
        if (hospital_pic == '/images/ylm.png'){
            alert("请上传医院图片");
            return;
        }
        if (departmentStr == ''){
            alert("请选择科室");
            return;
        }
        if (dutyStr == ''){
            alert("请选择职称");
            return;
        }
        if (hospital_desc == ''){
            alert("请输入医院介绍");
            return;
        }
        if (process == ''){
            alert("请输入建卡流程");
            return;
        }

        var params = {hospital_name:hospital_name,hospital_phone:hospital_phone,hospital_levelid:hospital_levelid,
            province_id:province_id,city_id:city_id,area_id:area_id,departmentStr:departmentStr,dutyStr:dutyStr,
            hospital_pic:hospital_pic,address:address,hospital_desc:hospital_desc,process:process,newcard_ability:newcard_ability};
        //console.log("保存医院的参数",params);
        bootbox.confirm("确定添加该医院?",function(result) {
            if (result) {
                IO.get("/api/addhosptial", params, function (data) {
                    bootbox.alert(data.msg, function () {
                        if (data.code == 0) {
                            location.href = '/96053/hospital';
                        }
                    })
                })

            }
        })
    });
    $("#addHosptialModal").on("hidden.bs.modal", function() {
        //$(this).removeData("bs.modal");
        location.href = '/96053/hospital';
    });

    //医院详情
    $(document).on("click",".detial",function(){
        var act_id=$(this).attr("act_id");
        $("#myModal").modal();
        IO.get("/api/hosptialdetial",{act_id:act_id},function(data){

            var provinceList = JSON.parse($("#province").val());
            //console.log("省市区的数据在这里",provinceList);

            //省市区的code转名称
            for(var x in provinceList){
                if (provinceList[x].code == data.data.province_id){
                    $("#province_id").val(provinceList[x].name);
                    //console.log("这个省",provinceList[x].name);
                    for(var y in provinceList[x].cityList){
                        var citys = provinceList[x].cityList;
                        if (citys[y].code == data.data.city_id){
                            //console.log("这个市",citys[y].name);
                            $("#city_id").val(citys[y].name);
                            for (var z in citys[y].areaList){
                                var area = citys[y].areaList;
                                if (area[z].code == data.data.area_id){
                                    //console.log("这个地区",area[z].name);
                                    $("#area_id").val(area[z].name);
                                }
                            }
                        }
                    }
                }
            }
            //console.log(data)
            $("#hospitalname").val(data.data.hospital_name);
            $("#hospital_phone").val(data.data.hospital_phone);
            $("#hospital_levelid").val(data.data.hospital_level.hospital_levelname);
            var ability = data.data.newcard_ability;
            if (ability == 1){
                ability = '小卡';
            }else if (ability == 2){
                ability = '大卡';
            }else{
                ability = '大卡、小卡';
            }
            $("#hospital_ability").val(ability);
            $("#Detialhospital_Pic").attr("src",data.data.hospital_pic);
            $("#DetialhospitalAddress").val(data.data.address);
            document.getElementById("DetialhospitalInstro").value = data.data.hospital_desc;
            document.getElementById("DetialhospitalProcess").value = data.data.process;
            //$("#province_id").val(data.data.province_id);
            //$("#city_id").val(data.data.city_id);
            //$("#area_id").val(data.data.area_id);
            //console.log("data.data.departmentlist",data.data);
            var departmentlist='';
            for (var x = 0; x< data.data.departmentlist.length;x++){
                departmentlist = departmentlist+data.data.departmentlist[x].department_name+";";
            }
            //console.log("科室的数据",departmentlist);
            var dutylist = '';
            for (var x = 0; x< data.data.dutylist.length;x++){
                dutylist = dutylist+data.data.dutylist[x].duty_name+";";
            }
            //console.log("职称的数据",dutylist);
            $("#departmentlist").val(departmentlist);
            $("#dutylist").val(dutylist);

        });

        //注册医生的表格
        $(function () {
            var grid;
            //每列数据列性
            var columns = [
                {
                    title: '姓名',
                    dataIndex: 'realname',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span>" + data[datarow].realname + "</span>";
                    }
                },
                {
                    title: '科室',
                    dataIndex: 'department_name',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span>" + data[datarow].department.department_name + "</span>";
                    }
                },
                {
                    title: '职称',
                    dataIndex: 'duty_name',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span>" + data[datarow].duty.duty_name + "</span>";
                    }
                },

            ];


            //数组初始化

            function dataInit() {
                grid = new Grid("/api/hosptialdetial/doctors?act_id="+act_id, $("#table_my_toast2"), $("#paging_my_toast2"), 1, 10, columns, get_input_param(), 10);

                //初始化
                grid.Init();
            }
            //获取输入框内容
            function get_input_param() {

                var content=$("#citiao_content").val();
                var param="content=" +content;
                return param;
            }
            dataInit();

        });

        //弹出添加医院
        $("#addhosptial").bind("click", function () {
            $("#addHosptialModal").modal();
        });

        //删除医院
        $(document).on("click","#deletehospital",function(){
        //$("#deletehospital").bind("click",function(){
            var params = {act_id:act_id};
            //console.log("参数",params);
            bootbox.confirm("确定删除该医院?",function(result) {
                if (result) {
                    IO.get("/api/deletehospital", params, function (data) {
                        bootbox.alert(data.msg, function () {
                            if (data.code == 0) {
                                location.href = '/96053/hospital';
                            }
                        })
                    })

                }
            })
        });

    });

    //医院编辑的省市区的三级联动
    $("#category9").on("change",function(){
        //console.log($(this).val());
        document.reg_testdate3.category102.innerHTML='请选择';
        document.reg_testdate3.category102.options.add(new Option("请选择","请选择"));
        document.reg_testdate3.category113.innerHTML='请选择';
        document.reg_testdate3.category113.options.add(new Option("请选择","请选择"));
        var provinceList = JSON.parse($("#province").val());
        var obj;
        for (obj in provinceList){
            if(provinceList[obj].code == $(this).val()){
                //console.log("测试1 ",$(this).val());
                //console.log("测试2",provinceList[obj].cityList);
                for (var x in provinceList[obj].cityList){
                    var city = provinceList[obj].cityList;
                    document.reg_testdate3.category102.options.add(new Option(city[x].name,city[x].code));
                    //console.log("城市们",city[x].name);
                }
            }
        }
    })
    $("#category10").on("change",function(){
        //console.log("市",$(this).val());
        //console.log("哈哈",$("#category9 option:selected").text());
        var province = $("#category9 option:selected").text();
        document.reg_testdate3.category113.innerHTML='请选择';
        document.reg_testdate3.category113.options.add(new Option("请选择","请选择"));
        var provinceList = JSON.parse($("#province").val());
        var obj;
        for (obj in provinceList){
            if(provinceList[obj].name == province){
                //console.log("测试1 ",$(this).val());
                //console.log("测试2",provinceList[obj].cityList);
                var city = provinceList[obj].cityList;
                for (var x in city){
                    if ($("#category10 option:selected").text()==city[x].name){
                        var area = city[x].areaList;
                        for (var y in area){
                            document.reg_testdate3.category113.options.add(new Option(area[y].name,area[y].code));
                            //console.log("城市们",area[y].name);
                        }

                    }

                }
            }
        }
    })

    //获取科室
    function getDepatments(depatments){
        //console.log("获取科室",depatments);
        $("#departments2").empty();
        for(var i=0;i<depatments.length;i++){
            var newoption = '';
            //console.log(depatments[i].department_name,depatments[i].department_id);
            newoption = '<div  style="float: left;padding-right: 10px"><p><input type="checkbox" checked="true" name="department2" is_del=false value="'+depatments[i].department_id+'" />'+depatments[i].department_name+'</p></div>';
            $("#departments2").append(newoption);
        }

    }

    //获取职称
    function getDutys(dutys){
        //console.log(dutys);
        $("#dutys2").empty();
        for(var i=0;i<dutys.length;i++){
            var newoption = '';
            //console.log(dutys[i].duty_name);
            newoption = '<div  style="float: left;padding-right: 10px"><p><input type="checkbox" checked="true" name="duty2" is_del=false value="'+dutys[i].duty_id+'" />'+dutys[i].duty_name+'</p></div>';
            $("#dutys2").append(newoption);
        }

    }

    //医院编辑
    $(document).on("click",".edit",function(){
        var act_id=$(this).attr("act_id");
        //console.log("医院ID",act_id);
        $("#edithospital_id").attr("value",act_id);
        $("#editHosptialModal").modal();
        IO.get("/api/hosptialdetial",{act_id:act_id},function(data){

            var provinceList = JSON.parse($("#province").val());

            //console.log("省市区的数据在这里",provinceList);
            var province_id = data.data.province_id;
            var city_id = data.data.city_id;
            //console.log("province_id",province_id);
            //获取市的下拉框选项
            document.reg_testdate3.category102.innerHTML='请选择';
            document.reg_testdate3.category102.options.add(new Option("请选择","请选择"));
            for (var obj in provinceList){
                    if(provinceList[obj].code == province_id){
                    for (var x in provinceList[obj].cityList){
                        var city = provinceList[obj].cityList;
                        document.reg_testdate3.category102.options.add(new Option(city[x].name,city[x].code));
                    }
                }
            }

            //获取区的下拉框选项
            document.reg_testdate3.category113.innerHTML='请选择';
            document.reg_testdate3.category113.options.add(new Option("请选择","请选择"));
            for (var  obj in provinceList){
                if(provinceList[obj].code == province_id){
                    //console.log("测试1 ",$(this).val());
                    //console.log("测试2",provinceList[obj].cityList);
                    var city = provinceList[obj].cityList;
                    for (var x in city){
                        if (city[x].code == city_id){
                            var area = city[x].areaList;
                            for (var y in area){
                                document.reg_testdate3.category113.options.add(new Option(area[y].name,area[y].code));
                                //console.log("城市们",area[y].name);
                            }

                        }

                    }
                }
            }

            //下拉框选中该医院所在的省市区
            var provinces = $("#category9");
            var citys = $("#category10");
            var areas = $("#category11");
            for(var x=1;x<provinces[0].length;x++ ){
                if(provinces[0][x].value==province_id){
                    provinces[0][x].selected = true;
                    continue;
                }
            }

            for(var y=1;y<citys[0].length;y++ ){
                if(citys[0][y].value==city_id){
                    citys[0][y].selected = true;
                    continue;
                }
            }
            for(var z=1;z<areas[0].length;z++ ){
                if(areas[0][z].value==data.data.area_id){
                    areas[0][z].selected = true;
                    continue;
                }
            }

            //下拉框选中该医院的医院级别
            var levels = $("#category12");
            for(var i=1;i<levels[0].length;i++ ){
                if(levels[0][i].value==data.data.hospital_levelid){
                    //console.log('haha',provinces[0][i].text);
                    levels[0][i].selected = true;
                }
            }

            //下拉框选中该医院的建卡能力
            var levels = $("#category86");
            for(var i=1;i<levels[0].length;i++ ){
                if(levels[0][i].value==data.data.newcard_ability){
                    //console.log('haha',provinces[0][i].text);
                    levels[0][i].selected = true;
                }
            }

            //显示医院的名字和电话
            $("#edithospitalname").val(data.data.hospital_name);
            $("#edithospitalphone").val(data.data.hospital_phone);

            //照片\地址\医院介绍\建卡流程
            $("#upimgs4").attr("src",data.data.hospital_pic);
            $("#EdithospitalAddress").val(data.data.address);
            document.getElementById("hospitalInstro2").value = data.data.hospital_desc;
            document.getElementById("hospitalProcess2").value = data.data.process;
            getDepatments(data.data.departmentlist);
            getDutys(data.data.dutylist);
            $("#departmentlist").val(departmentlist);
            $("#dutylist").val(dutylist);

        });
    });

    //添加编辑医院里的科室
    $(".addDepartment").bind("click", function () {
        $("#addDepartments2").modal();
        //$("#departments").html('<div style="float: left;padding-left: 10px"><p><input type="checkbox" name="department" value="" />口腔科 </p></div>');

    });
    $("#commitDepartments2").bind("click",function(){
        var newoption = '';
        newoption = '<div style="float: left;padding-right: 10px"><p><input type="checkbox" name="department2" value="" is_del=false />'+$("#addDepartmentName2").val()+' </p></div>';
        $("#departments2").append(newoption);
    })

    //添加编辑医院里的职称
    $(".addDuty").bind("click", function () {
        $("#addDutys2").modal();
        //$("#departments").html('<div style="float: left;padding-left: 10px"><p><input type="checkbox" name="department" value="" />口腔科 </p></div>');

    });
    $("#commitDutys2").bind("click",function(){
        var newoption = '';
        newoption = '<div style="float: left;padding-right: 10px"><p><input type="checkbox" name="duty2" value="" is_del=false />'+$("#addDutyName2").val()+' </p></div>';
        $("#dutys2").append(newoption);
    })

    //医院编辑保存
    $("#commitEditHosptial").bind("click",function(){
        var hospital_pic = $("#upimgs4").attr("src");
        var address = $("#EdithospitalAddress").val();
        var hospital_desc = document.getElementById("hospitalInstro2").value;
        var process = document.getElementById("hospitalProcess2").value;
        var hospital_name = $("#edithospitalname").val();
        var hospital_phone = $("#edithospitalphone").val();
        var hospital_levelid = $("#category12 option:selected").attr("data-id");
        var newcard_ability = $("#category86 option:selected").attr("data-id");
        var province_id = $("#category9 option:selected").attr("data-id");
        var city_id = $("#category10 option:selected").val();
        var area_id = $("#category11 option:selected").val();
        var department = document.getElementsByName("department2");
        var departmenObjs =[];
        var dutyObjs =[];
        var departmentCount = 0;
        var dutyCount = 0;
        for (var i = 0;i <department.length;i++){
            if(department[i].checked){
                var departmenObj = {department_id:department[i].value,department_name:department[i].nextSibling.nodeValue,is_del:$(department[i]).attr("is_del")};
                //console.log(departmenObj)
                departmenObjs.push(departmenObj);
                departmentCount ++ ;
            }
            else {
                $(department[i]).attr("is_del",true);
                if(!(department[i].value == '')){
                    var departmenObj = {department_id:department[i].value,department_name:department[i].nextSibling.nodeValue,is_del:$(department[i]).attr("is_del")};
                    departmenObjs.push(departmenObj);
                }
            }
        }
        var duty = document.getElementsByName("duty2");
        for (var i = 0;i <duty.length;i++){
            if(duty[i].checked){
                var dutyObj = {duty_id:duty[i].value,duty_name:duty[i].nextSibling.nodeValue,is_del:$(duty[i]).attr("is_del")};
                dutyObjs.push(dutyObj);
                dutyCount ++ ;
            }
            else {
                $(duty[i]).attr("is_del",true);
                if(!(duty[i].value == '')){
                    var dutyObj = {duty_id:duty[i].value,duty_name:duty[i].nextSibling.nodeValue,is_del:$(duty[i]).attr("is_del")};
                    dutyObjs.push(dutyObj);
                }
            }
        }
        if (hospital_name == ''){
            alert("请填写医院名称");
            return;
        }
        if (hospital_phone == ''){
            alert("请填写医院电话");
            return;
        }
        if ($("#category12 option:selected").attr("data-id") == undefined){
            alert("请选择医院级别");
            return;
        }
        if (newcard_ability == undefined){
            alert("请选择建卡能力");
            return;
        }
        if ($("#category9 option:selected").attr("data-id") == undefined){
            alert("请选择医院所在省");
            return;
        }
        if ($("#category10 option:selected").val() == '请选择'){
            alert("请选择医院所在市");
            return;
        }
        if ($("#category11 option:selected").val() == '请选择'){
            alert("请选择医院所在区");
            return;
        }
        if (departmentCount == 0){
            alert("请选择科室");
            return;
        }
        if (dutyCount == 0){
            alert("请选择职称");
            return;
        }
        if (hospital_desc == ''){
            alert("请输入医院介绍");
            return;
        }
        if (process == ''){
            alert("请输入建卡流程");
            return;
        }
        var hospital_id =  $("#edithospital_id").val()
        var params = {hospital_id:hospital_id,hospital_name:hospital_name,hospital_phone:hospital_phone,hospital_levelid:hospital_levelid,
            province_id:province_id,city_id:city_id,area_id:area_id,departmentlist:JSON.stringify(departmenObjs),dutylist:JSON.stringify(dutyObjs),
            hospital_pic:hospital_pic,address:address,hospital_desc:hospital_desc,process:process,newcard_ability:newcard_ability};
        //console.log("编辑医院的参数",params);
        bootbox.confirm("确定要保存该编辑?",function(result) {
            if (result) {
                IO.get("/api/edithosptial", params, function (data) {
                    bootbox.alert(data.msg, function () {
                        if (data.code == 0) {
                            location.href = '/96053/hospital';
                        }
                    })
                })

            }
        })
    });

});